﻿namespace EasyXaf.EntityDesigner.CodeTemplates.Extensions;

public static class StringExtensions
{
    public static string[] ToLines(this string value)
    {
        var text = value?.Replace("\r\n", "\n");
        return string.IsNullOrWhiteSpace(text) ? [] : text.Split(["\n"], StringSplitOptions.RemoveEmptyEntries);
    }

    public static string ToSingleLine(this string value)
    {
        var text = value?.Replace("\r\n", "\n");
        return string.IsNullOrWhiteSpace(text) ? null : string.Join("\\r\\n", [.. text.Split(["\n"], StringSplitOptions.None)]);
    }

    public static string ToFirstLower(this string value)
    {
        if (!string.IsNullOrWhiteSpace(value))
        {
            return value.Length == 1 ? value.ToLower() : value[0].ToString().ToLower() + value[1..];
        }
        return value;
    }

    public static string ToKebabCase(this string value)
    {
        var result = string.Empty;
        var chars = value.ToCharArray();
        foreach (var c in chars)
        {
            if (c >= 'A' && c <= 'Z')
            {
                result += $"-{(char)(c + 32)}";
            }
            else
            {
                result += c;
            }
        }
        return result.TrimStart('-');
    }
}
